<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学习笔记小站</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link rel="stylesheet" href="style.css">
</head>
<body class="bg-gray-50">
    <div id="app" class="min-h-screen">
        <!-- 头部导航 -->
        <header class="bg-white shadow-sm border-b">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
                <div class="flex justify-between items-center h-16">
                    <h1 class="text-2xl font-bold text-gray-900">
                        <i class="fas fa-book-open mr-2 text-blue-600"></i>
                        学习笔记小站
                    </h1>
                    <div class="flex items-center space-x-4">
                        <button @click="showSearch = !showSearch" class="text-gray-600 hover:text-gray-900">
                            <i class="fas fa-search"></i>
                        </button>
                        <button @click="exportData" class="text-gray-600 hover:text-gray-900">
                            <i class="fas fa-download"></i>
                        </button>
                        <button @click="importData" class="text-gray-600 hover:text-gray-900">
                            <i class="fas fa-upload"></i>
                        </button>
                        <button @click="showNewNote = true" class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700">
                            <i class="fas fa-plus mr-2"></i>新建笔记
                        </button>
                    </div>
                </div>
            </div>
        </header>

        <!-- 搜索栏 -->
        <div v-if="showSearch" class="bg-white border-b px-4 py-3">
            <div class="max-w-7xl mx-auto">
                <input 
                    v-model="searchQuery" 
                    type="text" 
                    placeholder="搜索笔记标题、内容或标签..."
                    class="w-full px-4 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
                >
            </div>
        </div>

        <!-- 主内容区 -->
        <main class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
            <!-- 标签过滤 -->
            <div class="mb-6">
                <div class="flex flex-wrap gap-2">
                    <button 
                        @click="selectedTag = ''" 
                        :class="['px-3 py-1 rounded-full text-sm', selectedTag === '' ? 'bg-blue-600 text-white' : 'bg-gray-200 text-gray-700']"
                    >
                        全部
                    </button>
                    <button 
                        v-for="tag in allTags" 
                        :key="tag"
                        @click="selectedTag = tag"
                        :class="['px-3 py-1 rounded-full text-sm', selectedTag === tag ? 'bg-blue-600 text-white' : 'bg-gray-200 text-gray-700']"
                    >
                        {{ tag }}
                    </button>
                </div>
            </div>

            <!-- 笔记列表 -->
            <div class="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
                <div 
                    v-for="note in filteredNotes" 
                    :key="note.id"
                    class="bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow cursor-pointer"
                    @click="editNote(note)"
                >
                    <div class="p-6">
                        <h3 class="text-lg font-semibold text-gray-900 mb-2">{{ note.title }}</h3>
                        <p class="text-gray-600 text-sm mb-3 line-clamp-3">{{ note.content.substring(0, 100) }}...</p>
                        <div class="flex flex-wrap gap-1 mb-3">
                            <span 
                                v-for="tag in note.tags" 
                                :key="tag"
                                class="px-2 py-1 bg-gray-100 text-gray-600 text-xs rounded"
                            >
                                {{ tag }}
                            </span>
                        </div>
                        <div class="text-xs text-gray-500">
                            {{ formatDate(note.createdAt) }}
                        </div>
                    </div>
                </div>
            </div>

            <!-- 空状态 -->
            <div v-if="filteredNotes.length === 0" class="text-center py-12">
                <i class="fas fa-book text-6xl text-gray-300 mb-4"></i>
                <h3 class="text-lg font-medium text-gray-900 mb-2">还没有笔记</h3>
                <p class="text-gray-500">开始记录你的学习之旅吧！</p>
                <button @click="showNewNote = true" class="mt-4 bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700">
                    创建第一篇笔记
                </button>
            </div>
        </main>

        <!-- 新建/编辑笔记弹窗 -->
        <div v-if="showNewNote || editingNote" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center p-4 z-50">
            <div class="bg-white rounded-lg max-w-4xl w-full max-h-[90vh] overflow-hidden">
                <div class="p-6 border-b">
                    <h2 class="text-xl font-semibold">{{ editingNote ? '编辑笔记' : '新建笔记' }}</h2>
                </div>
                <div class="p-6 overflow-y-auto max-h-[calc(90vh-120px)]">
                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-2">标题</label>
                        <input 
                            v-model="currentNote.title" 
                            type="text" 
                            class="w-full px-3 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
                            placeholder="输入笔记标题"
                        >
                    </div>
                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-2">标签</label>
                        <input 
                            v-model="tagInput" 
                            type="text" 
                            class="w-full px-3 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
                            placeholder="输入标签，用逗号分隔"
                            @keyup.enter="addTag"
                        >
                        <div class="flex flex-wrap gap-1 mt-2">
                            <span 
                                v-for="tag in currentNote.tags" 
                                :key="tag"
                                class="px-2 py-1 bg-blue-100 text-blue-700 text-sm rounded flex items-center"
                            >
                                {{ tag }}
                                <button @click="removeTag(tag)" class="ml-1 text-blue-500 hover:text-blue-700">
                                    <i class="fas fa-times text-xs"></i>
                                </button>
                            </span>
                        </div>
                    </div>
                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-2">内容</label>
                        <div class="border rounded-md">
                            <div class="border-b p-2 flex space-x-2">
                                <button @click="insertCode" class="px-3 py-1 text-sm bg-gray-100 rounded hover:bg-gray-200">
                                    <i class="fas fa-code mr-1"></i>插入代码
                                </button>
                                <button @click="insertBold" class="px-3 py-1 text-sm bg-gray-100 rounded hover:bg-gray-200">
                                    <i class="fas fa-bold mr-1"></i>加粗
                                </button>
                                <button @click="insertItalic" class="px-3 py-1 text-sm bg-gray-100 rounded hover:bg-gray-200">
                                    <i class="fas fa-italic mr-1"></i>斜体
                                </button>
                            </div>
                            <textarea 
                                v-model="currentNote.content" 
                                class="w-full h-96 p-4 resize-none focus:outline-none"
                                placeholder="支持 Markdown 格式&#10;&#10;示例：&#10;# 标题&#10;## 子标题&#10;- 列表项&#10;```javascript&#10;console.log('Hello World');&#10;```"
                            ></textarea>
                        </div>
                    </div>
                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-2">预览</label>
                        <div class="border rounded-md p-4 bg-gray-50 min-h-32">
                            <div v-html="renderMarkdown(currentNote.content)" class="prose max-w-none"></div>
                        </div>
                    </div>
                </div>
                <div class="p-6 border-t flex justify-end space-x-3">
                    <button @click="cancelEdit" class="px-4 py-2 text-gray-700 border rounded-md hover:bg-gray-50">
                        取消
                    </button>
                    <button @click="saveNote" class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700">
                        保存
                    </button>
                </div>
            </div>
        </div>

        <!-- 文件输入 -->
        <input type="file" id="fileInput" accept=".json" style="display: none" @change="handleFileImport">
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/marked/9.1.6/marked.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/prism/1.29.0/prism.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/prism/1.29.0/components/prism-javascript.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/prism/1.29.0/components/prism-css.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/prism/1.29.0/components/prism-markdown.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.4.21/vue.global.min.js"></script>
    <script src="storage.js"></script>
    <script src="app.js"></script>
</body>
</html>
